package cn.tedu.shoot;

import javax.swing.*;
import java.awt.*;

public class Bullet {
    int x,y;
    //static修饰的属性，是类级别的，所有的对象共享
    static Image bulletImg;
    static int speed;
    //静态代码块，在类加载的时候执行一次，而且只执行一次，在每次创建对象时不会再执行
    static{
        bulletImg = new ImageIcon("plane/bullets.png").getImage();
        speed = 2;
    }
    public Bullet (int x, int y){
        this.x = x;
        this.y = y;
    }
    public void move(){
        this.y-=speed;
    }
    //打中敌机
    public boolean hitEnemy(Enemy enemy){

        //可以得到子弹的坐标，this.x this.y
        int bX = this.x;
        int bY = this.y;
        //可以得到敌机的坐标 enemy.x enemy.y
        int eX1 = enemy.x;
        int eY1 = enemy.y;
        int eX2 = enemy.x + enemy.enemyImg.getWidth(null);
        int eY2 = enemy.y + enemy.enemyImg.getHeight(null);
        if(bX >= eX1 && bX <= eX2 && bY >= eY1 && bY <= eY2 ){
            return true;
        }
        return false;
    }


}
